home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / strtok.c < prev    next >
C/C++ Source or Header  |  1991-11-27  |  2KB  |  85 lines

  1. /*  $Revision: 1.2 $
  2. **
  3. **  This file has been modified to get it to compile more easily
  4. **  on pre-4.4BSD systems.  Rich $alz, June 1991.
  5. */
  6. #define NULL 0
  7.  
  8. /*
  9.  * Copyright (c) 1988 Regents of the University of California.
  10.  * All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms are permitted
  13.  * provided that: (1) source distributions retain this entire copyright
  14.  * notice and comment, and (2) distributions including binaries display
  15.  * the following acknowledgement:  ``This product includes software
  16.  * developed by the University of California, Berkeley and its contributors''
  17.  * in the documentation or other materials provided with the distribution
  18.  * and in all advertising materials mentioning features or use of this
  19.  * software. Neither the name of the University nor the names of its
  20.  * contributors may be used to endorse or promote products derived
  21.  * from this software without specific prior written permission.
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  23.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25.  */
  26.  
  27. #if 0
  28. #if defined(LIBC_SCCS) && !defined(lint)
  29. static char sccsid[] = "@(#)strtok.c    5.7 (Berkeley) 6/1/90";
  30. #endif /* LIBC_SCCS and not lint */
  31.  
  32. #include <stddef.h>
  33. #include <string.h>
  34. #endif
  35.  
  36. char *
  37. strtok(s, delim)
  38.     register char *s, *delim;
  39. {
  40.     register char *spanp;
  41.     register int c, sc;
  42.     char *tok;
  43.     static char *last;
  44.  
  45.  
  46.     if (s == NULL && (s = last) == NULL)
  47.         return (NULL);
  48.  
  49.     /*
  50.      * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  51.      */
  52. cont:
  53.     c = *s++;
  54.     for (spanp = delim; (sc = *spanp++) != 0;) {
  55.         if (c == sc)
  56.             goto cont;
  57.     }
  58.  
  59.     if (c == 0) {        /* no non-delimiter characters */
  60.         last = NULL;
  61.         return (NULL);
  62.     }
  63.     tok = s - 1;
  64.  
  65.     /*
  66.      * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  67.      * Note that delim must have one NUL; we stop if we see that, too.
  68.      */
  69.     for (;;) {
  70.         c = *s++;
  71.         spanp = delim;
  72.         do {
  73.             if ((sc = *spanp++) == c) {
  74.                 if (c == 0)
  75.                     s = NULL;
  76.                 else
  77.                     s[-1] = 0;
  78.                 last = s;
  79.                 return (tok);
  80.             }
  81.         } while (sc != 0);
  82.     }
  83.     /* NOTREACHED */
  84. }
  85.